package com.own.business.message.center.business.service.impl;

import com.own.business.message.center.business.service.MessageCenterGroupChildService;
import com.own.business.message.center.entity.bo.MessageCenterGroupChildBo;
import com.own.business.message.center.entity.map.MessageCenterGroupChildMapVo;
import com.own.business.message.center.entity.po.MessageCenterGroupChild;
import com.own.business.message.center.entity.query.MessageCenterGroupChildQuery;
import com.own.business.message.center.entity.vo.MessageCenterGroupChildVo;
import com.own.business.message.center.mapper.MessageCenterGroupChildMapper;
import com.own.component.common.base.service.impl.AbstractBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * MessageCenterGroupChildServiceImpl
 *
 * @author chenxueli
 * @date 2023-11-13 18:17:40
 */
@Slf4j
@Service
public class MessageCenterGroupChildServiceImpl extends AbstractBaseService<
        MessageCenterGroupChild,
        MessageCenterGroupChildBo,
        MessageCenterGroupChildVo,
        MessageCenterGroupChildMapVo,
        MessageCenterGroupChildQuery,
        MessageCenterGroupChildMapper
        > implements MessageCenterGroupChildService {

    @Resource
    private MessageCenterGroupChildMapper messageCenterGroupChildMapper;

    /**
     * 根据消息key列表分组查询
     *
     * @param messageKeyList 消息key列表
     * @return 分组列表
     */
    @Override
    public Map<String, Set<String>> mapGroupKeyListByMessageKeyList(Collection<String> messageKeyList) {
        if (messageKeyList == null || messageKeyList.isEmpty()) {
            return new HashMap<>();
        }
        var wrapper = lambdaQueryWrapper().in(MessageCenterGroupChild::getMessageKey, messageKeyList);
        var list = messageCenterGroupChildMapper.selectList(wrapper);
        return list.stream().collect(Collectors.groupingBy(MessageCenterGroupChild::getGroupKey, Collectors.mapping(MessageCenterGroupChild::getMessageKey, Collectors.toSet())));
    }

    /**
     * 根据分组key查询消息key列表
     *
     * @param groupKey 分组key
     * @return 消息key列表
     */
    @Override
    public List<String> listMessageKeyByGroupKey(String groupKey) {
        var wrapper = lambdaQueryWrapper().select(MessageCenterGroupChild::getMessageKey).eq(MessageCenterGroupChild::getGroupKey, groupKey);
        var list = messageCenterGroupChildMapper.selectList(wrapper);
        return list.stream().map(MessageCenterGroupChild::getMessageKey).collect(Collectors.toList());
    }

}
